package com.northwest_man.refactor_tool;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 找出 other 文件与 root 文件之间占位符的差异 以 root 文件作为基准
 */
public class FindPlaceholder {
    // private static final String[] dirs = new String[]{"values", "values-de",
    // "values-el", "values-es", "values-fr",
    // "values-it", "values-nl", "values-pl", "values-pt", "values-ru", "values-sv",
    // "values-tr"};
    private static final String[] dirs = new String[] { "values", "values-ar", "values-id","values-bn", "values-fr", "values-pt", "values-zh" };

    private static final String[] shouldSkipKeys = new String[] {  "one_hundred_percent_guarantee"};

    private static void findPlaceholderAccording2EnglishLangFile() throws IOException {
        for (final String dir : dirs) {
            findPlaceholderAccording2EnglishLangFile(dir, new File(Paths.get(other , dir , "strings.xml" ).toString() ));
        }
    }

    public static void main() throws IOException {
        findPlaceholderAccording2EnglishLangFile();
    }

    public static void main(String[] args) throws IOException {
        findPlaceholderAccording2EnglishLangFile();
    }

    private static final String root = Paths.get(  Constants.PAT_PROJECT_DIR,"module_resource","src" ,"main","res","values")   .toString() ;
    // private static final String other = Constants.PAT_PROJECT_DIR +
    // "/app/src/main/res/";
    private static final String other =Paths.get(  Constants.PAT_PROJECT_DIR,"module_resource","src" ,"main","res" )   .toString()  ;

    private static boolean shouldSkip(final String key) {
        boolean shouldSkip = false;
        for (final String shouldSkipKey : shouldSkipKeys) {
            if (shouldSkipKey.equals(key)) {
                shouldSkip = true;
                break;
            }
        }
        return shouldSkip;
    }

    private static void findPlaceholderAccording2EnglishLangFile(final String which, final File otherStringFile)
            throws IOException {
        // %(\d+\$)?([-#+ 0,(\<]*)?(\d+)?(\.\d+)?([tT])?([a-zA-Z%])
        final String regex = "%(\\d+\\$)?([-#+ 0,(\\<]*)?(\\d+)?(\\.\\d+)?([tT])?([a-zA-Z%])";
        final String regex2 = Constants.STRING_ITEM_ROW_REGEX;
        final String enFileContent = FileUtils.readFileToString(new File(Paths.get(root,   "strings.xml").toString() ));
        final Pattern pattern = Pattern.compile(regex);

        final Pattern pattern2 = Pattern.compile(regex2);
        final Matcher matcher2 = pattern2.matcher(enFileContent);
        // 英语文件中含有占位符的 key
        final List<KV> valueWithPlaceholderKeyList = new ArrayList<>();
        while (matcher2.find()) {
            final String key = matcher2.group(1);
            final String value = matcher2.group(2);
            final Matcher matcher = pattern.matcher(value);
            final List<String> placeholderList = new ArrayList<>();
            while (matcher.find()) {
                placeholderList.add(matcher.group());
            }
            if (!placeholderList.isEmpty()) {
                valueWithPlaceholderKeyList.add(new KV(key, value, placeholderList));
            }
        }
        final String otherFileContent = FileUtils.readFileToString(otherStringFile);
        final Matcher _matcher2 = pattern2.matcher(otherFileContent);
        final JsonArray jsArr = new JsonArray();
        while (_matcher2.find()) {
            final String key = _matcher2.group(1);
            final String value = _matcher2.group(2);
            for (final KV preKey : valueWithPlaceholderKeyList) {
                if (key.trim().equals(preKey.key.trim())) {

                    final Matcher _matcher = pattern.matcher(value);
                    final List<String> placeholderList = new ArrayList<>();
                    while (_matcher.find()) {
                        placeholderList.add(_matcher.group());
                    }
                    if (placeholderList.isEmpty()) {
                        if (!shouldSkip(key)) {
                            final JsonObject jsObj = new JsonObject();
                            jsObj.addProperty("key", key);
                            jsObj.addProperty("old_value", preKey.value);
                            jsObj.addProperty("cur_value", value);
                            jsArr.add(jsObj);
                        }
                    } else {
                        // 新文件中的该字段也包含占位符
                        if (value.trim().equals(preKey.value.trim())) {

                        } else {
                            final List<String> oldPlaceholderList = preKey.placeholderList;
                            if (oldPlaceholderList.size() != placeholderList.size()) {
                                if (!shouldSkip(key)) {
                                    final JsonObject jsObj = new JsonObject();
                                    jsObj.addProperty("key", key);
                                    jsObj.addProperty("old_value", preKey.value);
                                    jsObj.addProperty("cur_value", value);
                                    jsArr.add(jsObj);
                                }
                            } else {
                                final int size = oldPlaceholderList.size();
                                boolean allSame = true;
                                for (int i = 0; i < size; i++) {
                                    final String oldPlaceholder = oldPlaceholderList.get(i);
                                    final String curPlaceholder = placeholderList.get(i);
                                    if (!(oldPlaceholder.equals(curPlaceholder))) {
                                        allSame = false;
                                        break;
                                    }
                                }
                                if (!allSame) {
                                    if (!shouldSkip(key)) {
                                        final JsonObject jsObj = new JsonObject();
                                        jsObj.addProperty("key", key);
                                        jsObj.addProperty("old_value", preKey.value);
                                        jsObj.addProperty("cur_value", value);
                                        jsArr.add(jsObj);
                                    }
                                }
                            }
                        }

                    }
                    break;
                }
            }
        }
        System.out.println(String.format("--------------------%20s--------------------", which));
        System.out.println(jsArr);
        System.out.println(String.format("--------------------%20s--------------------", which));
    }

    private static class KV {
        public final String key;
        public final String value;
        public final List<String> placeholderList;

        private KV(final String key, final String value, final List<String> placeholderList) {
            this.key = key;
            this.value = value;
            this.placeholderList = placeholderList;
        }
    }

}
